//
//  UIButton+buttonStyle.m
//  RM_IM
//
//  Created by user1 on 2018/7/2.
//  Copyright © 2018年 user1. All rights reserved.
//

#import "UIButton+buttonStyle.h"

@implementation UIButton (buttonStyle)


- (void)layoutButtonWithEdgeInsetsStyle:(ButtonEdgeInsetsImageStyle)style imageTitleSpace:(CGFloat)space{
    
    CGFloat imageWidth = self.imageView.frame.size.width;
    CGFloat imageHeight = self.imageView.frame.size.height;
    
    CGFloat labelWidth = self.titleLabel.frame.size.width;
    CGFloat labelHeight = self.titleLabel.frame.size.height;
    
    CGFloat imageOriginx = self.imageView.frame.origin.x;
    CGFloat titleOriginx = self.titleLabel.frame.origin.x;
    
    // imageView 和titleLabel之间的间距
    CGFloat margin = titleOriginx - imageOriginx - imageWidth;
    
    UIEdgeInsets imageEdgeInsets = UIEdgeInsetsZero;
    UIEdgeInsets labelEdgeInsets = UIEdgeInsetsZero;
    
    switch (style) {
        case ButtonEdgeInsetsImageStyleTop:
        {
            imageEdgeInsets = UIEdgeInsetsMake(-0.5*labelHeight, 0.5*labelWidth+0.5*margin+imageOriginx, 0.5*(labelHeight+space),0.5*labelWidth-0.5*margin);
            labelEdgeInsets = UIEdgeInsetsMake(0.5*(imageHeight+space), -0.5*imageWidth , -0.5*imageHeight, 0.5*imageWidth);
        }
            break;
        case ButtonEdgeInsetsImageStyleLeft:
        {
            imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0.5*space);
            labelEdgeInsets = UIEdgeInsetsMake(0, 0.5*space, 0, 0);
        }
            break;
        case ButtonEdgeInsetsImageStyleRight:
        {
            imageEdgeInsets = UIEdgeInsetsMake(0, labelWidth+space/2.0, 0, -labelWidth-space/2.0);
            labelEdgeInsets = UIEdgeInsetsMake(0, -imageWidth-space/2.0, 0, imageWidth+space/2.0);
        }
            break;
        case ButtonEdgeInsetsImageStyleBottom:
        {
            imageEdgeInsets = UIEdgeInsetsMake(0.5*(labelHeight+space), 0.5*labelWidth+imageOriginx, -0.5*labelHeight,0.5*labelWidth);
            labelEdgeInsets = UIEdgeInsetsMake(-0.5*imageHeight, -0.5*imageWidth , 0.5*(imageHeight+space), 0.5*imageWidth);
        }
            break;
        default:
            break;
    }
    self.titleEdgeInsets = labelEdgeInsets;
    self.imageEdgeInsets = imageEdgeInsets;
}


@end
